package com.xiaoxin.common;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @version v1.0
 * @Description: mybatis plus 代码生成器
 * @Author: TongRui乀
 * @Date: 2021/11/22 5:10 下午
 */
public class MybatisPlusGenerator {

    public static void main(String[] args) {

        var dbConfig = new DataSourceConfig
                .Builder("jdbc:mysql://127.0.0.1:3306/xiaoxin_single","admin","admin");

        String rootPath = System.getProperty("user.dir") + "/src/main/java";
        String mapperXmlPath = System.getProperty("user.dir") + "/src/main/resources/mapper";
        String parentPackage = "com.xiaoxin";
        FastAutoGenerator.create(dbConfig)
                // 全局配置
                .globalConfig((scanner, builder) -> builder.author("TongRui")
                        .outputDir(rootPath) // 代码输出地址
                        .disableOpenDir()  // 禁止生成代码后打开文件
                        .fileOverride()
                )
                // 包配置
                .packageConfig((scanner, builder) -> builder.parent(parentPackage)
                        // 配置mapping目录
                        .pathInfo(Collections.singletonMap(OutputFile.mapperXml, mapperXmlPath))
                )
                // 策略配置
                .strategyConfig((scanner, builder) ->
                        builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                                // Controller类策略
                                .controllerBuilder()
                                    .enableRestStyle()
                                    .enableHyphenStyle()
                                // Entity类策略
                                .entityBuilder()
                                    .enableLombok() // 启用Lombok注解
                                    .idType(IdType.ASSIGN_ID) // 主键类型 使用自定义自增ID
                                .build()
                )
                .execute();

    }
    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
